package com.clouds.wms.cms;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.clouds.wms.bean.Cmsmd;
import com.clouds.wms.bean.Cmsml;
import com.clouds.wms.exception.SysException;
import com.clouds.wms.service.CmsmlService;
import com.clouds.wms.util.RequestHelper;

@Controller
@RequestMapping("/cmsml")
public class CmsmlController {
	private Log log = LogFactory.getLog(getClass());
	
	@Resource(name="cmsmlService")
	private CmsmlService cmsmlService;
	/**
	 * 列出所有的角色
	 * @param request
	 * @param response
	 * @return
	 * @throws SysException
	 */
	@RequestMapping(value = "/cmsmlList.xhtml")
	public String cmsmlList(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		String mlab = RequestHelper.getString(request, "mlab");
		List<Cmsml> list = cmsmlService.getCmsmlListByMlab(mlab);
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = null;
		String callback = request.getParameter("callback");
		try {
			out = response.getWriter();
			JSONObject datas = new JSONObject();
			datas.put("data", list);
			out.write(callback + "(" + datas.toString() + ");");
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return null;
	}
	/**
	 * 员工PDA权限管理
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/pdaRole.xhtml")
	public String pdaRole(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException, IOException {
		String mlab = request.getParameter("pdaHidMdaa");
		String[] daaaUpdate = request.getParameterValues("daaaUpdate");
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		List<Cmsml> syseList = new ArrayList<Cmsml>();
		if(daaaUpdate != null && daaaUpdate.length > 0) {
			for(int i = 0; i < daaaUpdate.length; i++) {
				int mlac  = Integer.parseInt(daaaUpdate[i]);
				if(mlac != 0){
					Cmsml cmsml = new Cmsml();
					cmsml.setMlab(mlab);
					cmsml.setMlac(mlac);
					syseList.add(cmsml);
				}
			}
		}
		cmsmlService.saveCmsmls(mlab, syseList);
		out.print("success");
		return null;
	}
}
